package de.koller.balance.scene.bumpCube;

import de.koller.aframe.gl.ATexture;
import de.koller.aframe.gl.Camera;
import de.koller.aframe.gl.obj.Quad;
import de.koller.aframe.util.noise.n2D.Noise2D;
import de.koller.aframe.util.noise.n2D.Perlin2D;
import de.koller.balance.render.BRenderExt;
import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.math.RVec3;
import de.koller.oceanFramework.util.ColorIntUtil;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class BumpCubeSzene extends BRenderExt {
	
	private final RVec3 lightVector = new RVec3( 1f, .2f, .8f );
	private final RVec3 eyevector = new RVec3( 0, 0, 1f );
	private Random rnd = new Random();
	private ATexture perlinBump;
	private Quad quad = Quad.createXPlane( -1, 1, 0, 2, 2 ).setTextureLayout( 1, 1 );
	
	public final Camera camera = new Camera();

	public BumpCubeSzene() {
		Perlin2D.PRINT_PROGRESS = true;
		perlinBump = new NoiseTexture( new Perlin2D( 0, 6, 0.75f, 2, rnd ) );
	}
	
	private void initCamera() {
		commonUses.setUpPerspective();
		
		GL11.glPushMatrix();
		//camera.position.set( 0.8f, 1.8f, 1.8f );
		camera.position.set( 0, 0, 2 );
		camera.setLookAt( new RVec3( 0, 0, 0 ) );
		camera.up.set( 0, 1, 0 );
		camera.transformGL();
	}
	
	private void popCamera() {
		GL11.glPopMatrix();
	}

	@Override
	public void renderSzene() {
		GL11.glClearColor( 1, 1, 1, 1 );
		initCamera();
		
		GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE );
		GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE );
		
		GL11.glPushMatrix();
		
		GL11.glTranslatef( 0, 0, 0 );
		perlinBump.bindAnisotrophic();
		quad.renderStructure();
		
		GL11.glPopMatrix();
		
		popCamera();
	}
	
	private class NoiseTexture extends ATexture implements ATexture.ITextureBuilder {

		private Noise2D noise;

		public NoiseTexture( Noise2D noise ) {
			this.noise = noise;
		}
		
		@Override
		protected void setUp( TextureParameters tparam ) {	
			tparam.builder = this;
			tparam.magFilter = GL11.GL_LINEAR;
			tparam.size = 1024;
		}

		@Override
		public void build( ParticleTexture texture ) {
			noise.normalize( .5f );
//			for( int x = 0; x < noise.noise.length; x++ )
//				for( int y = 0; y < noise.noise.length; y++ )
//					noise.noise[x][y] *= 3f;
			
			//Function3DPreview.showFunction( 0, 0, 1, 1, noise );
			
			for( int x = 0; x < texture.getWidth(); x++ ) {
				for( int y = 0; y < texture.getHeight(); y++ ) {
					RVec3 n = noise.getDerivate( 1f/texture.getWidth() * x, 1f/texture.getHeight() * y );
					n.normalize();
					
					int r = (int)( 128f * n.x ) + 128;
					int g = (int)( 128f * n.y ) + 128;
					int b = (int)( 128f * n.z ) + 128;
					texture.setPixel( ColorIntUtil.getIntColor( 0xff, r, g, b ), x, y );
					
					//System.out.println( n );
//					int a = 0xff902020;
//					
//					int d = ColorOperations.diffuseLight( 
//							lightVector,
//							n,
//							0xffffffff,
//							0xffffffff );
//					
//					int s = ColorOperations.specularLight(
//							lightVector,
//							n,
//							eyevector,
//							0xff606060,
//							0xff606060,
//							10 );
//							
//					texture.setPixel( ColorOperations.lightColor( 0xff6c0996, a, d, s ), x, y );
				}
				
				System.out.println( x + " / " + texture.getWidth() );
			}

		}
		
	}
	
}
